import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import 'package:ducafe_ui_core/ducafe_ui_core.dart';

/// Toast提示类型枚举
enum ToastType {
  success, // 成功
  error, // 失败
  warning, // 警告
}

/// Toast提示组件
class ToastWidget {
  /// 显示成功提示
  static void showSuccess(String message, {Duration? duration}) {
    _showToast(message: message, type: ToastType.success, duration: duration);
  }

  /// 显示失败提示
  static void showError(String message, {Duration? duration}) {
    _showToast(message: message, type: ToastType.error, duration: duration);
  }

  /// 显示警告提示
  static void showWarning(String message, {Duration? duration}) {
    _showToast(message: message, type: ToastType.warning, duration: duration);
  }

  /// 内部方法：显示Toast
  static void _showToast({
    required String message,
    required ToastType type,
    Duration? duration,
  }) {
    // 根据类型设置不同的颜色和图标
    Color backgroundColor;
    Color textColor;
    IconData icon;

    switch (type) {
      case ToastType.success:
        backgroundColor = const Color(0xFF4CAF50); // 绿色
        textColor = Colors.white;
        icon = Icons.check_circle;
        break;
      case ToastType.error:
        backgroundColor = const Color(0xFFF44336); // 红色
        textColor = Colors.white;
        icon = Icons.error;
        break;
      case ToastType.warning:
        backgroundColor = const Color(0xFFFF9800); // 橙色
        textColor = Colors.white;
        icon = Icons.warning;
        break;
    }

    // 创建自定义Toast Widget
    Widget toastWidget = Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
      decoration: BoxDecoration(
        color: backgroundColor,
        borderRadius: BorderRadius.circular(8.r),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.2),
            blurRadius: 8.r,
            offset: Offset(0, 2.h),
          ),
        ],
      ),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(icon, color: textColor, size: 20.sp),
          SizedBox(width: 8.w),
          Flexible(
            child: Text(
              message,
              style: TextStyle(
                color: textColor,
                fontSize: 14.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
        ],
      ),
    );

    // 显示Toast
    showToastWidget(
      toastWidget,
      duration: duration ?? const Duration(seconds: 2),
      position: ToastPosition.top,
      animationCurve: Curves.easeInOut,
      animationDuration: const Duration(milliseconds: 300),
      handleTouch: true,
    );
  }

  /// 显示自定义Toast
  static void showCustom({
    required Widget widget,
    Duration? duration,
    ToastPosition? position,
  }) {
    showToastWidget(
      widget,
      duration: duration ?? const Duration(seconds: 2),
      position: position ?? ToastPosition.bottom,
      animationCurve: Curves.easeInOut,
      animationDuration: const Duration(milliseconds: 300),
    );
  }

  /// 显示简单文本Toast
  static void showText(
    String message, {
    Duration? duration,
    ToastPosition? position,
    Color? backgroundColor,
    Color? textColor,
  }) {
    showToast(
      message,
      duration: duration ?? const Duration(seconds: 2),
      position: position ?? ToastPosition.bottom,
      backgroundColor: backgroundColor ?? Colors.black87,
      textStyle: TextStyle(color: textColor ?? Colors.white, fontSize: 14.sp),
      radius: 8.r,
    );
  }
}

/// Toast扩展方法，方便使用
extension ToastExtension on String {
  /// 显示成功Toast
  void showSuccess({Duration? duration}) {
    ToastWidget.showSuccess(this, duration: duration);
  }

  /// 显示失败Toast
  void showError({Duration? duration}) {
    ToastWidget.showError(this, duration: duration);
  }

  /// 显示警告Toast
  void showWarning({Duration? duration}) {
    ToastWidget.showWarning(this, duration: duration);
  }

  /// 显示简单文本Toast
  void showToast({
    Duration? duration,
    ToastPosition? position,
    Color? backgroundColor,
    Color? textColor,
  }) {
    ToastWidget.showText(
      this,
      duration: duration,
      position: position,
      backgroundColor: backgroundColor,
      textColor: textColor,
    );
  }
}
